﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display {
    import flash.events.*;
    import flash.display.*;
    import Classes.*;

    public class ContextMenu extends Sprite {

        public static const ITEM_WIDTH:int = 100;
        public static const ITEM_HEIGHT:int = 20;

        protected static var m_currentMenu:ContextMenu;

        protected var m_style:Object;
        protected var m_items:Array;
        protected var m_menuBackground:Shape;

        public function ContextMenu(_arg1:Array, _arg2:Object=null){
            var _local4:Object;
            this.m_style = {
                contextMenu:{
                    backgroundColor:EmbeddedArt.TEXT_BACKGROUND_COLOR,
                    borderColor:EmbeddedArt.BORDER_MAIN_COLOR,
                    borderWidth:1
                },
                contextMenuItem:{
                    width:ITEM_WIDTH,
                    height:ITEM_HEIGHT,
                    font:EmbeddedArt.DEFAULT_FONT_NAME,
                    fontSize:12,
                    textAlign:"left",
                    padding:2,
                    color:EmbeddedArt.TEXT_MAIN_COLOR,
                    backgroundColor:EmbeddedArt.TEXT_BACKGROUND_COLOR,
                    hoverColor:EmbeddedArt.TEXT_BACKGROUND_COLOR,
                    hoverBackgroundColor:EmbeddedArt.TEXT_MAIN_COLOR
                }
            };
            super();
            this.m_items = [];
            var _local3:int;
            while (_local3 < _arg1.length) {
                _local4 = _arg1[_local3];
                if ((((((_local4 is Object)) && (_local4.hasOwnProperty("label")))) && (_local4.hasOwnProperty("action")))){
                    this.m_items.push(new ContextMenuItem(_local4.label, _local4.action, ((_local4.style) || (this.m_style))));
                } else {
                    if ((_local4 is ContextMenuItem)){
                        this.m_items.push(_local4);
                    } else {
                        throw (new ArgumentError("items array can only contain objects with label and action properties or ContextMenuItems"));
                    };
                };
                _local3++;
            };
            this.setStyle(_arg2);
            this.draw();
            this.addEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler, false, 0, true);
        }
        public function show(_arg1:Number, _arg2:Number):void{
            if (ContextMenu.m_currentMenu != null){
                ContextMenu.m_currentMenu.hide();
            };
            this.x = _arg1;
            this.y = _arg2;
            Global.stage.addChild(this);
            ContextMenu.m_currentMenu = this;
            UI.updateCurrentCursor(true);
        }
        public function hide():void{
            this.removeListeners();
            if (Global.stage == this.parent){
                Global.stage.removeChild(this);
            };
            ContextMenu.m_currentMenu = null;
        }
        protected function setStyle(_arg1:Object):void{
            var _local2:String;
            var _local3:String;
            if (_arg1){
                for (_local2 in _arg1) {
                    this.m_style[_local2] = ((this.m_style[_local2]) || ({}));
                    for (_local3 in _arg1[_local2]) {
                        this.m_style[_local2][_local3] = _arg1[_local2][_local3];
                    };
                };
            };
        }
        protected function draw():void{
            var _local1:ContextMenuItem;
            var _local5:int;
            while (this.numChildren > 0) {
                this.removeChildAt(0);
            };
            var _local2:int;
            var _local3:int = this.m_style.contextMenuItem.height;
            var _local4:int = this.m_style.contextMenuItem.padding;
            for each (_local1 in this.m_items) {
                _local1.y = ((_local2 * _local3) + (_local2 * _local4));
                this.addChild(_local1);
                _local2 = (_local2 + 1);
            };
            this.m_menuBackground = new Shape();
            _local5 = this.m_style.contextMenu.borderWidth;
            this.m_menuBackground.graphics.lineStyle(_local5, this.m_style.contextMenu.backgroundColor);
            this.m_menuBackground.graphics.beginFill(this.m_style.contextMenu.borderColor);
            this.m_menuBackground.graphics.drawRoundRect(-(_local5), -(_local5), (this.width + _local5), (this.height + _local5), (_local5 * 4), (_local5 * 4));
            this.m_menuBackground.graphics.endFill();
            this.addChildAt(this.m_menuBackground, 0);
            for each (_local1 in this.m_items) {
                _local1.draw();
            };
        }
        protected function addedToStageHandler(_arg1:Event):void{
            this.removeEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler);
            this.addListeners();
        }
        protected function mouseOverHandler(_arg1:MouseEvent):void{
            var _local2:ContextMenuItem;
            if ((((_arg1.target.parent == this)) && (_arg1.target.hasOwnProperty("state")))){
                for each (_local2 in this.m_items) {
                    if (_local2 != _arg1.target){
                        _local2.state = ContextMenuItem.STATE_UP;
                    } else {
                        _local2.state = ContextMenuItem.STATE_OVER;
                    };
                };
            };
        }
        protected function mouseClickHandler(_arg1:MouseEvent):void{
            this.hide();
        }
        protected function stageMouseClickHandler(_arg1:MouseEvent):void{
            if (!this.hitTestPoint(Global.stage.mouseX, Global.stage.mouseY, true)){
                this.hide();
            };
        }
        protected function stageMouseMoveHandler(_arg1:MouseEvent):void{
            var _local2:ContextMenuItem;
            if (!this.hitTestPoint(Global.stage.mouseX, Global.stage.mouseY, true)){
                for each (_local2 in this.m_items) {
                    _local2.state = ContextMenuItem.STATE_UP;
                };
            };
        }
        protected function addListeners():void{
            this.addEventListener(MouseEvent.CLICK, this.mouseClickHandler, false, 0, true);
            this.addEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler, false, 0, true);
            if (((this.parent) && (!((this.parent["constructor"] == this["constructor"]))))){
                Global.stage.addEventListener(MouseEvent.CLICK, this.stageMouseClickHandler, false, 0, true);
                Global.stage.addEventListener(MouseEvent.MOUSE_OUT, this.stageMouseMoveHandler, false, 0, true);
            };
        }
        protected function removeListeners():void{
            this.removeEventListener(MouseEvent.CLICK, this.mouseClickHandler);
            this.removeEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler);
            if (((this.parent) && (!((this.parent["constructor"] == this["constructor"]))))){
                this.stage.removeEventListener(MouseEvent.CLICK, this.stageMouseClickHandler);
            };
        }

    }
}//package Display 
